﻿package cn.antscript.airCMS.display.component.btnbase 
{
	import cn.antscript.airCMS.data.singleData.AbstractData;
	import cn.antscript.airCMS.display.component.EditPanel;
	import cn.antscript.airCMS.display.component.SurePanel;
	import cn.antscript.airCMS.display.component.WaitBar;
	import cn.antscript.airCMS.display.MusicListener;
	import cn.antscript.airCMS.display.VideoViewer;
	import cn.antscript.model.SqlModel;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author antscript
	 */
	public class FileBtn extends Sprite
	{
		public var data:AbstractData;
		
		private var _bitmap:Bitmap;
		
		private var _editBtn:AbstractIco;
		private var _imgBtn:AbstractIco;
		private var _replaceBtn:AbstractIco;
		private var _delBtn:AbstractIco;
		
		private var _txt:TextField;
		
		
		public function FileBtn(data_:AbstractData=null,bitmapdata:BitmapData=null,delEnabled:Boolean=true,container:DisplayObjectContainer=null,xPos:int=0,yPos:int=0) 
		{	
			
			data = data_ as AbstractData;
			
			
			this.graphics.lineStyle(2, 0x000000);
			this.graphics.drawRect(0, 0, 120, 120);
			this.graphics.endFill();
			
			
			_bitmap = new Bitmap(bitmapdata);
			_bitmap.x = 35;
			_bitmap.y = 40;
			addChild(_bitmap);
			
			_txt = new TextField();
			_txt.x = 0;
			_txt.y = 0;
			_txt.width = 120;
			_txt.height = 20;
			_txt.background = true;
			_txt.backgroundColor = 0x000000;
			_txt.text = data.label;
			_txt.textColor = 0x999999;
			_txt.selectable = false;
			addChild(_txt);			
			
			
			if (container != null) {
				container.addChild(this);
			}
			this.x = xPos;
			this.y = yPos;
			
			
			
			_editBtn = new AbstractIco(IcoBitmapdatas.Edit, "编辑", this, 0, 130);
			
			switch(_bitmap.bitmapData) {
				case IcoBitmapdatas.MusicType:
				_imgBtn = new AbstractIco(IcoBitmapdatas.Music, "播放", this, 30, 130);
				break;
				case IcoBitmapdatas.VideoType:
				_imgBtn = new AbstractIco(IcoBitmapdatas.Video, "播放", this, 30, 130);
				break;
				default:
				break;
			}
			
			_replaceBtn = new AbstractIco(IcoBitmapdatas.Replace, "替换", this, 63, 130);
			
			//if (delEnabled) {
				_delBtn = new AbstractIco(IcoBitmapdatas.Delete, "删除", this, 97, 130);
			//}		
			
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
			
			this.addEventListener(MouseEvent.CLICK, onClick);
			this.addEventListener(MouseEvent.ROLL_OVER, onOver);
			this.addEventListener(MouseEvent.ROLL_OUT, onOut);
		}
		private function onClick(e:MouseEvent):void 
		{
			switch(e.target) {
				case _editBtn:
				EditPanel.getInstance().edit(data, this.stage);
				break;
				case _delBtn:
				SurePanel.getInstance().show("提示：一经删除则不可恢复，确定删除？",this.stage,del);
				break;				
				case _imgBtn:
				if (_bitmap.bitmapData == IcoBitmapdatas.MusicType) {
					MusicListener.getInstance().play(data["url"],this.stage);
				}
				else if(_bitmap.bitmapData == IcoBitmapdatas.VideoType){
					VideoViewer.getInstance().play(data["url"]);
				}
				break;
				case _replaceBtn:
				dispatchEvent(new Event("replace"));
				break;
			}
			
		}
		private function del():void
		{
			WaitBar.getInstance().show(this.stage);
			SqlModel.getInstance().del(data,delCom);
		}
		
		private function delCom():void
		{
			WaitBar.getInstance().hide();
			dispatchEvent(new Event("deleted"));
		}
		private function onRemove(e:Event):void 
		{
			this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemove);
			this.removeEventListener(MouseEvent.CLICK, onClick);
			this.removeEventListener(MouseEvent.ROLL_OVER, onOver);
			this.removeEventListener(MouseEvent.ROLL_OUT, onOut);
			
			_bitmap.bitmapData = null;
			
			data = null;
			
		}		
		
		private function onOver(e:MouseEvent):void 
		{
			this.graphics.clear();
			this.graphics.beginFill(0);
			this.graphics.drawRect(0, 0, 120, 120);
			this.graphics.endFill();
			
			if (_txt.width < _txt.textWidth) {
				addEventListener(Event.ENTER_FRAME, onEnter);
			}
		}
		private function onEnter(e:Event):void
		{			
			_txt.scrollH += 1;
			if (_txt.scrollH == _txt.maxScrollH) {
				removeEventListener(Event.ENTER_FRAME, onEnter);
			}
		}
		private function onOut(e:MouseEvent):void 
		{
			this.graphics.clear();
			this.graphics.lineStyle(2, 0x000000);
			this.graphics.drawRect(0, 0, 120, 120);
			this.graphics.endFill();
			
			_txt.scrollH = 0;			
			removeEventListener(Event.ENTER_FRAME, onEnter);
		}
		
		
	}

}